let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    nav_select: false, // 快捷导航
    array: [{
      name: "男",
      checked: false
    }, {
      name: "女",
      checked: true
    }],
    name: '',
    region: '',
    phone: '',
    detail: '',
    descs: '',
    error: '',
    shengao: '',
    tizhong: '',
    imgs: [],
    imglist: [],
    config: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    App._post_form('wxapi/config', {}, function (result) {
      _this.setData({
        config: result.data
      });

    });

  },

  /**
   * 表单提交
   */
  saveData: function (e) {
    let _this = this,
      values = e.detail.value
    values.region = _this.data.region;

    var token = wx.getStorageSync("token");
    if(!token){
      wx.navigateTo({
        url: '../login/login',
      });
      return;
    }
    
    // 记录formId
    // App.saveFormId(e.detail.formId);

    // 表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }

    values.userid = wx.getStorageSync('user_id');
    values.imglist = this.data.imglist.join(",")
    console.log("ss", values);

 
    wx.requestSubscribeMessage({
      tmplIds: ['5bJAe6LAh8jQZ1N4Jx0lv7eKfqZOCm6zj_FiRR2VUSg','DvPA2GZD9UYfw_yH2F9EhhbsXb6inWCjr_tNZhx7_hI'],
      success(res) {
        console.log("res", res);
        // 提交到后端
        App._post_form('wxapi/person/add', values, function (result) {
          if (result.status == 200) {

            wx.showModal({
              title: '系统提示',
              showCancel:false,
              content:  result.message,
              success (res) {
                if (res.confirm) {
                  _this.setData({
                    name: "",
                    phone: "",
                    shengao: "",
                    tizhong: "",
                    content: "",
                    imgs: []
                  })
                } else if (res.cancel) {
                 
                }
              }
            })

     
          } else {

    
            wx.showModal({
              title: '系统提示',
              showCancel:false,
              content:  result.message,
              success (res) {
                if (res.confirm) {
                 
                } else if (res.cancel) {
                 
                }
              }
            })

            

          }


        }, false, function () {
          // 解除禁用

        });

      }
    })



  },

  /**
   * 表单验证
   */
  validation: function (values) {
    console.log("values", values);
    if (values.name === '') {
      this.data.error = '姓名不能为空';
      return false;
    }
    if (values.phone.length === '') {
      this.data.error = '手机号不能为空';
      return false;
    }
    console.log("values.phone.length", values.phone.length);
    if (values.phone.length !== 11) {
      this.data.error = '手机号长度有误';
      return false;
    }
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!reg.test(values.phone)) {
      this.data.error = '手机号不符合要求';
      return false;
    }

    if (this.data.imgs.length == 0) {
      this.data.error = '请上传照片';
      return false;
    }

    return true;
  },

  /**
   * 修改地区
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  radiochange: function (e) {
    console.log('radio发生change事件，携带的value值为：', e.detail.value)
    this.setData({
      sex: e.detail.value
    })
  },
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;

    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        // console.log(imgs);
        that.setData({
          imgs: tempFilePaths
        });

        for (var i = 0; i < tempFilePaths.length; i++) {

          that.uplodafile(tempFilePaths[i]);
        }


      }
    });
  },
  uplodafile(filePath) {
    var that = this;
    wx.uploadFile({
      url: App.api_root + 'upload/file2',
      filePath: filePath,
      name: 'imgFile',
      success: function (res) {
        var data = JSON.parse(res.data);
        console.log("data", data.error)
        if (data.error == 0) {
          that.data.imglist.push(data.url);
        } else {


        }
      }
    })
  },
  showimg(event){
    console.log("ss",event);
    console.log("ss",event.currentTarget.dataset.src);


    var url  =event.currentTarget.dataset.src;
    // var index=0;
    // for(var i=0;i<this.data.imglist.length;i++){
    //   if(this.data.imglist[i]==url){
    //     index=i;
    //     break;
    //   }
    // }

    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: this.data.imgs // 需要预览的图片http链接列表
    })
  },
  onShareAppMessage: function () {
   
  },

  //用户点击右上角分享朋友圈
  onShareTimeline: function () {
  
  },

})